﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Linq;
using System.Linq;
using System.Web;
using OLAP.Models.Partials;
using OLAPFramework.Cache;
using OLAPFramework.Data;

namespace OLAP.Models
{
    public interface IPressreleasesRepository
    {
        List<Pressreleases> GetAllPressreleases(string sourceCode, int langId, int sourceId, string searchPhrase, DateTime? fromDate, DateTime? toDate, int pageItemFrom, int pageItemTo, out int totalRow, out List<Attachment> attachments);

        Attachment GetAttactmentById(long id);

        News GetPressreleaseById(long id);
    }

    public class PressreleasesRepository : IPressreleasesRepository
    {

        public List<Pressreleases> GetAllPressreleases(string sourceCode, int langId, int sourceId, string searchPhrase, DateTime? fromDate, DateTime? toDate, int pageItemFrom, int pageItemTo, out int totalRow, out List<Attachment> attachments)
        {
            using (NewsDatabase db = new NewsDatabase())
            {
                object[] param = new object[] { sourceCode, langId, sourceId, searchPhrase, fromDate, toDate, pageItemFrom, pageItemTo };
                IMultipleResults multipleResult = db.GetManyTables("spPressreleasesSelect", param);
                totalRow = multipleResult.GetResult<int>().FirstOrDefault();
                List<Pressreleases> outputResult = multipleResult.GetResult<Pressreleases>().ToList();
                attachments = multipleResult.GetResult<Attachment>().ToList();
                return outputResult;
            }
        }


        public Attachment GetAttactmentById(long id)
        {
            using (NewsDatabase db = new NewsDatabase())
            {
                return db.GetSingleRecord<Attachment>("spAttachmentSelectById", id);
            }
        }


        public News GetPressreleaseById(long id)
        {
            using (NewsDatabase db = new NewsDatabase())
            {
                News news = new News();
                object[] param = new object[] { id};
                IMultipleResults multipleResult = db.GetManyTables("spPressreleasesSelectById", param);
                news.Pressrelease = multipleResult.GetResult<Pressreleases>().FirstOrDefault();
                news.Attachments = multipleResult.GetResult<Attachment>().ToList();
                return news;
            }
        }
    }
}